package org.dreamwork.jasmine2.restful;

import com.google.gson.annotations.Expose;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 14-11-18
 * Time: 下午2:14
 */
class AbstractRestfulAPIDefinition {
    @Expose
    Map<String, ContextParameterDefinition> contextParameters = new HashMap<String, ContextParameterDefinition> ();
    @Expose
    protected String title;
    @Expose
    protected String pattern;
    @Expose
    protected String description;
    @Expose
    protected boolean forceEncrypt;
    @Expose
    protected boolean forceJson;

    void addContextParameter (ContextParameterDefinition parameter) {
        contextParameters.put (parameter.getName (), parameter);
    }

    boolean isRequiredContextParameter (String name) {
        return contextParameters.containsKey (name) && contextParameters.get (name).isRequired ();
    }

    boolean isContextParameter (String name) {
        return contextParameters.containsKey (name);
    }

    public Collection<ContextParameterDefinition> getContextParameters () {
        return contextParameters.values ();
    }

    public Collection<ContextParameterDefinition> getRequiredContextParameter () {
        Collection<ContextParameterDefinition> c = new HashSet<ContextParameterDefinition> ();
        for (ContextParameterDefinition p : RestfulAPIContext.parameters.values ()) {
            if (p.isRequired ()) {
                c.add (p);
            }
        }

        for (ContextParameterDefinition p : contextParameters.values ()) {
            if (p.isRequired ())
                c.add (p);
        }

        return c;
    }

    public String getTitle () {
        return title;
    }

    public String getDescription () {
        return description;
    }

    public String getPattern () {
        return pattern;
    }

    public boolean isForceEncrypt () {
        return forceEncrypt;
    }

    public boolean isForceJson () {
        return forceJson;
    }
}